#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import os
import platform

from common.common import invoke_rpc_tool_interface, report_job_details
from common.common_models import SubJobDetails
from common.const import RpcToolInterface
from oracle import logger
from oracle.common.constants import Platform

if platform.system().lower() != "windows":
    import pwd
    import grp
    from common.util.check_user_utils import check_os_user


def report_job_details_linux_windows(pid, job_id, sub_job_id, log_detail, task_status):
    job_detail = SubJobDetails(taskId=job_id, subTaskId=sub_job_id, progress=100,
                               logDetail=[log_detail], taskStatus=task_status)
    if platform.system().lower() == Platform.WINDOWS:
        invoke_rpc_tool_interface(job_id,
                                  RpcToolInterface.REPORT_JOB_DETAIL, job_detail.dict(by_alias=True))
    else:
        report_job_details(pid, job_detail)


def delete_matching_contain_pattern_files(path, contain_pattern):
    for root, _, files in os.walk(path):
        for file in files:
            if contain_pattern in file:
                file_path = os.path.join(root, file)
                os.remove(file_path)
                logger.info(f"Deleted: {file_path}")
